package domain.service.base;

import domain.dao.base.MaintainBaseDao;
import domain.entity.base.MaintainBaseInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by bowen on 2017-08-28.
 */
@Service
public class MaintainBaseService {

    @Autowired
    private MaintainBaseDao baseDao;

    @Transactional(value = "base", rollbackFor = Exception.class)
    public void create(List<MaintainBaseInfo> baseInfos) {
        this.delete();
        baseDao.create(baseInfos);
    }

    public void delete() {
        baseDao.delete();
    }

    public Map<String, List<MaintainBaseInfo>> getBaseFacilitys() {
        Map<String, List<MaintainBaseInfo>> baseInfoMap = new HashMap<String, List<MaintainBaseInfo>>();
        List<MaintainBaseInfo> baseInfos = baseDao.getBaseFacilitys();
        List<MaintainBaseInfo> facilityList = new ArrayList<MaintainBaseInfo>();
        List<MaintainBaseInfo> materielList = new ArrayList<MaintainBaseInfo>();
        if (baseInfos != null && baseInfos.size() > 0) {
            for (MaintainBaseInfo baseInfo : baseInfos) {
                if ("facility".equals(baseInfo.getType())) {
                    facilityList.add(baseInfo);
                } else {
                    materielList.add(baseInfo);
                }
            }
        }
        baseInfoMap.put("facility", facilityList);
        baseInfoMap.put("materiel", materielList);
        return baseInfoMap;
    }

    public List<MaintainBaseInfo> getBaseBasesByCheck(String type) {
        return baseDao.getBaseBasesByCheck(type);
    }

}
